package cn.kgc.persondemo01.controller;

import cn.kgc.persondemo01.pojo.Person;
import cn.kgc.persondemo01.pojo.ResultEntity;
import cn.kgc.persondemo01.service.PersonService;
import io.swagger.annotations.*;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@Api(tags = "人员信息接口",description = "提供商品相关的REST API")
public class PersonController {
    @Resource
    PersonService personService;

    @ApiOperation("查询人员信息接口")
    @RequestMapping("/selectPerson")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "人员编号"),
            @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "gender", value = "性别"),
            @ApiImplicitParam(name = "age", value = "年龄"),
            @ApiImplicitParam(name = "rank", value = "等级"),
            @ApiImplicitParam(name = "department", value = "所在部门"),
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数问题"),
            @ApiResponse(code = 404, message = "请求页面没有或页面跳转路径不对"),
            @ApiResponse(code = 405, message = "请求类型不正确"),
            @ApiResponse(code = 500, message = "后台程序报错")
    })
    public Map<String, Object> selectPerson() {
        Map<String, Object> map = new HashMap<>();
        map.put("person", personService.selectAll());
        return map;
    }

    @PostMapping(value = "/save")
    @ApiOperation("新增成员接口")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数问题"),
            @ApiResponse(code = 404, message = "请求页面没有或页面跳转路径不对"),
            @ApiResponse(code = 405, message = "请求类型不正确"),
            @ApiResponse(code = 500, message = "后台程序报错")
    })
    public ResultEntity<Integer> save(@ApiParam(name = "person", value = "人员信息", required = true) @RequestBody Person person) {
        Integer integer = personService.savePerson(person);
        return ResultEntity.successWithoutData(integer);
    }

    @ApiOperation("根据编号删除接口")
    @RequestMapping("/delPersonById")
    @ApiImplicitParam(name = "id", value = "人员编号")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数问题"),
            @ApiResponse(code = 404, message = "请求页面没有或页面跳转路径不对"),
            @ApiResponse(code = 405, message = "请求类型不正确"),
            @ApiResponse(code = 500, message = "后台程序报错")
    })
    public ResultEntity<Integer> delPersonById(Integer id) {
        Integer person = personService.delPerson(id);
        return ResultEntity.successWithoutData(person);
    }

}